import axios from "axios";
import { ElMessage } from 'element-plus'

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
const service = axios.create({
  baseURL: "/api",
  timeout: 10000,
  withCredentials: true,
});

service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    console.log(error);
    Promise.reject(error);
  }
);

service.interceptors.response.use((response) => {
  const res = response.data;
  if (res.code !== 0) {
    ElMessage.error(res.msg)
    return Promise.reject(new Error(res.message || "Error"));
  } else {
    return res.data;
  }
});

export default service;
